package v2ch01.streams;

import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.util.stream.*;

/**
 * 作 者：liaojun 时 间: 2018年2月28日 上午10:07:05 说 明: 修改时间： 修改说明：
 */

public class PrimitiveTypeStreams {
	public static void show(String title, IntStream stream) {
		final int SIZE = 10;
		int[] firstElements = stream.limit(SIZE + 1).toArray();
		System.out.print(title + ": ");
		for (int i = 0; i < firstElements.length; i++) {
			if (i > 0) {
				System.out.print(", ");
			}
			if (i < SIZE) {
				System.out.print(firstElements[i]);
			} else {
				System.out.print("...");
			}
		}
		System.out.println();
	}
	
	public static void main(String[] args) throws IOException {
		IntStream is1 = IntStream.generate(() -> (int) (Math.random() * 100));
		show("is1", is1);
		IntStream is2 = IntStream.range(5, 10);
		show("is2", is2);
		IntStream is3 = IntStream.rangeClosed(5, 10);
		show("is3", is3);
		
		Path path = Paths.get("src/java8/gutenberg/alice30.txt");
		System.out.println(path.toAbsolutePath());//字符串拼接的路径，不管存不存在
		System.out.println(path.toRealPath());//真实路径，不存在会报错
		System.out.println(path.getNameCount());
		String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
		
		Stream<String> words = Stream.of(contents.split("\\PL+"));
		IntStream is4 = words.mapToInt(String::length);
		show("is4", is4);
		String sentence = "\uD835\uDD46 is the set of octonions.";
		System.out.println(sentence);
		IntStream codes = sentence.codePoints();
		System.out.println(codes.mapToObj(c -> String.format("%X ", c)).collect(Collectors.joining()));
		
		Stream<Integer> integers = IntStream.range(0, 100).boxed();
		IntStream is5 = integers.mapToInt(Integer::intValue);
		show("is5", is5);
	}
}
